#include "protomaster.h"
#include <thread>
#include <chrono>
// proto username@ip password ...


int main(int argc,char* argv[])
{
    const string ERROR_MSG = string("syntax: ") + argv[0] + 
	string(" <command> \n") + 
	string("Commands: \"copy <filename> <dest dir>\" ") +
	string("and \"rcmd <command>\"\n"); 
    int returnVal = 0;
    thread * run = NULL;    
    bool done = false;
    string com, s, ex, pass, addr, dest;
    //cout << argc << endl;


    if (argc > 3){
        string user_ip  = argv[1];
        string password = argv[2];
        string command  = argv[3];
        if (command == "copy"){
            if(argc == 6)
		    {
                string file     = argv[4];
                string destDir  = argv[5];
                
                

                /* Run some task on a new thread.
                   Make sure to set the done flag to true when finished. */
                run = new thread(&copyOnly,std::ref(file),std::ref(destDir)
                    ,std::ref(user_ip),std::ref(password),std::ref(done));
                

                // Print status.
                int count = 0;
                // Wait for 10 seconds for thread to finish                
                while(!done && count <= 100){
                    std::this_thread::sleep_for(std::chrono::milliseconds(100));
                    count++;
                    
                }
                if (!done){
                    system("killall sshpass");
                }
                

                run->join(); // Join thread.
		    }
		    else
		    {
			    cout << ERROR_MSG; 
		    }
        }
        else if(command == "rcmd"){

            
            string remoteCommand = "";
            
            for (int i = 4; i < argc; i++){
                remoteCommand += argv[i];
                remoteCommand += " ";
            }
	    remoteCommand = "\"" + remoteCommand + "\"";	    
            /* Run some task on a new thread.
                   Make sure to set the done flag to true when finished. */
            run = new std::thread(rcmd,std::ref(user_ip),std::ref(password),
                    std::ref(remoteCommand),std::ref(done));
            
            // Print status.
            int count = 0;
            // Wait for 10 seconds for thread to finish                
            while(!done && count <= 100){
                std::this_thread::sleep_for(std::chrono::milliseconds(100));
                count++;
                
            }
            if (!done){
                system("killall sshpass");
                returnVal = -5;
            }
            else{
                returnVal = 0;
            }
            

            run->join(); // Join thread.
        }
        else{
	    cout << ERROR_MSG;
        }
    }
    else{
        cout << ERROR_MSG;
    }    
    
    if (run != NULL){
        delete run;
    }
    return returnVal;
}
/*

if(argc > 2)
{
	com =  argv[1];
	s = argv[2];
	ifstream fin;
	fin.open("/home/alex/csr-repository/CommunicationLib/swarmlist.txt");	
	//fin.open("./swarmlist.txt");
	
	if(fin.fail())
	{
		cout<< fin.fail();
		fin.close();
		return 1;
	}
	if(com == "copy")
	{
		
	}
	if(com == "copyd")
	{
		if(argc == 4)
		{
			while(!fin.eof())
			{
				fin>> addr;
				if(fin.eof())
				{
					break;
				}
				fin>> pass;
				copyOnly(s, argv[3], addr, pass);
			}
		}
		else
		{
			for(int i = 1; !fin.eof(); i++)
			{
				fin>> addr;
				if(fin.eof())
				{
					break;
				}
				fin>> pass;
				if(i == atoi(argv[4]))
				{
					copyOnly(s, argv[3], addr, pass);
					break;
				}
			} 
		}
	}
	if(com == "comp")
	{
		if(argc >= 4)
		{
			ex = argv[3];
		}
		else
		{
			ex = "a.out";
		}
		bool all = (argc != 5);
		for(int i = 1; !fin.eof(); i++)
		{
			fin>> addr;
			if(fin.eof())
			{
				break;
			}
			fin>> pass;
			if(all || (i == atoi(argv[4])))
			{
				copyAndCompile(s, s, addr, pass, ex);
			}
		}
	}
	//cout << com == "rcmd" << argc == 4;
	if(com == "rcmd" && argc > 3)
	{
		string bot = argv[2];
		bool all = bot == "all";
		int botnum = atoi(bot.c_str());
		string rcom = "";
		for(int i = 3; i < argc; i++)
		{
			rcom += argv[i];
			rcom += " ";
		}
		for(int i = 1; !fin.eof(); i++)
		{
			fin>> addr;
			if(fin.eof())
			{
				break;
			}
			fin>> pass;
			if(all || (i == atoi(bot.c_str())))
			{
				cout << rcom << endl;
				rcmd(addr, pass, rcom);
			}
		}
	}
	if(com == "copydir" && argc >= 3)
	{
		if(argc == 4)
		{
			while(!fin.eof())
			{
				fin>> addr;
				if(fin.eof())
				{
					break;
				}
				fin>> pass;
				copyDir(s, argv[3], addr, pass);
			}
		}
		else
		{
			for(int i = 1; !fin.eof(); i++)
			{
				fin>> addr;
				if(fin.eof())
				{
					break;
				}
				fin>> pass;
				if(i == atoi(argv[4]))
				{
					copyDir(s, argv[3], addr, pass);
					break;
				}
			} 
		}
	}
	if(com == "alg" && argc == 3)
	{
		map<int, string> ips, passes;
		map<int, swarmSocketClient> clients;
		map<int, bool> cons;
		string robotaddress = "./csr-repository/SwarmHost/swarmlist.txt";
		robotConnect(clients, ips, passes, cons, robotaddress);
		if(s == "text")
		{
			textInterface(clients, ips, passes,cons, robotaddress);
		}
		if(s == "con")
		{
			s = "y";
			while(1)
			{
				if(s == "y")
				{
					//system("clear");
					printf("List of connected robots click\n");
					printf("robot 0 is all %i\n", (int) ips.size());
					for(int i = 1; i <= ips.size(); i++)
					{
						printf("robot %i is %s connected:%i\n", i, ips[i].c_str(), cons[i]);
					}
					printf("Refresh? y/n\n");
				}
				cin >> s;
				if(s == "n")
				{
					break;
				}
				robotReconnect(clients, ips, passes, cons);
			}
		}
	} 
	fin.close();
}

if(argc == 2)
{
	//s =  argv[1];
	s =  argv[1];
	s = "arm-bcm2708hardfp-linux-gnueabi-gcc -c " +s+ " -I./csr-repository/ImageProcLib -I./wiringPi/wiringPi -I./csr-repository/ImageProcLib/raspicam_cv -I./csr-repository/ImageProcLib/threshold -I/usr/local/include/ -I$HOME/csr-repository/include 2> ./results.txt";
	if(0 == system(s.c_str()))
	{
		s = argv[1];
		s = s.substr(0, s.find(".c"));
		s += ".o";
		int i = s.find_last_of("/");
		s = s.substr(i, s.size() - i);
		s = "mv ." + s + " ./PiObjects";
		cout<< endl << s << endl;
		system(s.c_str());
	}
	
}
//cout<< s.c_str();



return 0;
}
*/
